package online.lona.mc.bukkit.plugin.ymer;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;

public class Config {
    private static Config instance = null;
    private final String configPath = Ymer.PLUGIN_ROOT + "/config.yml";
    private FileConfiguration configuration;
    private int authTimeout = 10;
    private int authInterval = 55;
    private String timeoutMessage = "Ymer反作弊:客户端验证超时";
    private String authFailedMessage = "Ymer反作弊:客户端验证错误,请确定你安装的MOD受到允许";
    private String avatarKickMessage = "禁止使用影分身漏洞";
    private Config() {
        File configFile = new File(configPath);
        if (!configFile.exists()) {
            try {
                configFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            save(configFile);
        }
        configuration = YamlConfiguration.loadConfiguration(configFile);
        load();
    }

    public static void init() {
        instance = new Config();
    }

    public static Config getInstance() {
        return instance;
    }

    public void save(final File file) {
        YamlConfiguration yamlConfiguration = new YamlConfiguration();
        yamlConfiguration.set("authTimeout", authTimeout);
        yamlConfiguration.set("authInterval", authInterval);
        yamlConfiguration.set("authFailedMessage", authFailedMessage);
        yamlConfiguration.set("timeoutMessage", timeoutMessage);
        yamlConfiguration.set("avatarKickMessage", avatarKickMessage);
        try {
            yamlConfiguration.save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void load() {
        authTimeout = configuration.getInt("authTimeout", authTimeout);
        authInterval = configuration.getInt("authInterval", authInterval);
        authFailedMessage = configuration.getString("authFailedMessage", authFailedMessage);
        timeoutMessage = configuration.getString("timeoutMessage", timeoutMessage);
        avatarKickMessage = configuration.getString("avatarKickMessage", avatarKickMessage);
    }

    public int getAuthTimeout() {
        return authTimeout;
    }

    public int getAuthInterval() {
        return authInterval;
    }

    public String getTimeoutMessage() {
        return timeoutMessage;
    }

    public String getAuthFailedMessage() {
        return authFailedMessage;
    }

    public String getAvatarKickMessage() {
        return avatarKickMessage;
    }
}
